<?php

namespace app\admin\controller\statistics;

use app\admin\model\order\Rent as OrderRent;
use app\common\controller\Backend;
use think\exception\DbException;
use think\response\Json;

/**
 * 堆场统计
 *
 * @icon fa fa-circle-o
 */
class Yard extends Backend
{

    /**
     * Yard模型对象
     * @var \app\admin\model\statistics\Yard
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\statistics\Yard;
        $this->view->assign("organTypeList", $this->model->getOrganTypeList());
    }



    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }

        $real_where1=[];
        $real_where1['state']=1;
        $where1=[];
        $where1['organ_type']=1;

        [$where, $sort, $order, $offset, $limit] = $this->buildparams();

        // 排序
        $pageSize=$limit;

        $list = $this->model
            ->field('id,organ_name,organ_city')
            ->with([
                'yardbin'=>function($query)use($real_where1){$query->where($real_where1);},
            ])
            ->where($where)
            ->where($where1)
            ->order($sort, $order)
            ->select();
        $json = json_encode((object)$list);
        $lists = json_decode($json, true);

        foreach ($lists as $k=> &$vo){
            $vo['bin_number']=count($vo['yardbin']);
           /* $vo['bin_type_name']=$bin_type_name;
            $vo['bin_native_name']=$bin_native_name;
            if($params['custom']['bin_origin']==1){
                $vo['bin_origin_name']='自备箱';
            }else if ($params['custom']['bin_origin']==2){
                $vo['bin_origin_name']='租赁箱';
            }*/
        }

        $bin_number = array_column($lists,'bin_number');//返回数组中指定的一列
        $ids = array_column($lists,'id');//返回数组中指定的一列
        array_multisort($bin_number,SORT_DESC,$ids,SORT_ASC,$lists);//对多个数组或多维数组进行排序
        /*
         * 过滤箱子是空的数组
         */
        $filteredLists=array_filter($lists,function ($v){
            return !empty($v['yardbin']);
        });

        foreach ($filteredLists as $k=> &$vo){
            $filteredLists[$k]['can_use']=0;
            $filteredLists[$k]['pre_allot']=0;
            $filteredLists[$k]['pre_restore']=0;
            foreach ($vo['yardbin'] as $kk=> &$vv){
                if($vv['current_status']==1){
                    $filteredLists[$k]['can_use']=$filteredLists[$k]['can_use']+1;
                }elseif ($vv['current_status']==2){
                    $filteredLists[$k]['pre_allot']=$filteredLists[$k]['pre_allot']+1;
                }elseif ($vv['current_status']==3){
                    $filteredLists[$k]['pre_restore']=$filteredLists[$k]['pre_restore']+1;
                }
            }
        }
        //数组根据当前页码和每页数据获取当前页的数据
        $filteredData=array_slice($filteredLists,$offset,$pageSize);

        $result = ['total' => count($filteredLists), 'rows' => $filteredData];
        return json($result);
    }



}
